#include "precompile.h"

#include <vsys>

#include "EnumItemsCallback.h"
#include "Storage.h"
#include "dataptr.h"

#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif

////////////////////////////////////////////////////////////////////////////////////////////
MergeAllItems::MergeAllItems()
	: m_pArrIncreamentOpItem(NULL)
{
}

void MergeAllItems::operator()(const OpItem& opItem)
{
	m_pArrIncreamentOpItem->push_back(opItem);
}

SaveCraslIDAndStore::SaveCraslIDAndStore()
	: m_bIncreamentStoreFlag(false)
	, m_pIFConnection(NULL)
	, m_pStream(NULL)
	, m_pIncreamentStoreOptimize(NULL)
{
}

void SaveCraslIDAndStore::operator()(const UROptItem& item)
{
	vclsid classID;
	byte RestoreType;

	gtl::ArchiveTypeInOutOperator<objectid>::Save(m_pStream,item.m_oid);

	switch(item.m_kState)
	{
	case kAdd:
		RestoreType = 0x82;
		break;
	case kModify:
		RestoreType = 0x81;
		break;
	case kDelete:
		RestoreType = 0x80;
		break;
	}
	gtl::ArchiveTypeInOutOperator<byte>::Save(m_pStream, RestoreType);

	if(m_bIncreamentStoreFlag)	
		m_pIncreamentStoreOptimize->AppendItem(OpItem(item.m_oid,item.m_kState,false));

	if(RestoreType != 0x80)
	{
		classID = m_pIFConnection->GetClassID(item.m_oid);
		gtl::ArchiveTypeInOutOperator<vclsid>::Save(m_pStream, classID);

		sys::DataInterfacePtr<IDataEntity> pIFEnity(m_pIFConnection, item.m_oid);
		ASSERT(pIFEnity != NULL);
		pIFEnity->Serialize(false, m_pStream);
	}
}